package b4.view.component.basic
{
	import b4.util.basic.*;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.text.*;
	import flash.ui.Mouse;

	public class BasicButton extends Sprite
	{
		private var _disable:Boolean;
		private var titleText:TextField;
		
		public function BasicButton(BackgroundImage:Class, text:String = null, fontSize:int = 15, width:int = -1, height:int = -1)
		{
			super();
			//背景
			var backgroundImage:Sprite = new BackgroundImage();
			if (width > 0 && height > 0)
			{
				backgroundImage.width = width;
				backgroundImage.height = height;
			} 
			backgroundImage.mouseChildren = false;
			addChild(backgroundImage);
			if (text != null)
			{
				//标题名字
				titleText = new TextField();
				titleText.selectable = false;
	        	titleText.autoSize = TextFieldAutoSize.LEFT;
				titleText.defaultTextFormat = new TextFormat("微软雅黑", fontSize, 0x000000);
				titleText.text = text;
				titleText.x = (this.width - titleText.width) / 2;
				titleText.y = (this.height - titleText.height) / 2;
				addChild(titleText);
			}
			this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			_disable = false;
		}
		
		public function set disable(flag:Boolean):void
		{
			if (_disable != flag)
			{
				if (flag)
				{
					this.filters = [new ColorMatrixFilter(BFont.blackWhiteFilter)];
				}
				else
				{
					this.filters = null;
				}
				_disable = flag;
			}
		}
		
		public function get disable():Boolean
		{
			return _disable;
		}
		
		//设置按钮文字
		public function set text(t:String):void
		{
			if (titleText != null)
			{
				titleText.text = t;
				titleText.x = (this.width - titleText.width) / 2;
				titleText.y = (this.height - titleText.height) / 2;
			}
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			if (disable)
				return;
			Mouse.cursor = "button";
			this.filters = [new ColorMatrixFilter(BFont.brightFilter)];
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			if (disable)
				return;
			this.filters = null;
		}
	}
}